package com.fyj.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 钰杰
 * @className ThreadUtil
 * @description VX:FengYJ996
 * @date 2019/12/38:50
 */
@Slf4j
public class ThreadUtil implements Runnable {

    private Map dataSourceMap;  //数据源

    private List<String> resultList; //Insert语句集合

    @Override
    public void run() {
        try {
            JdbcUtils.insertTargetData(this.dataSourceMap, this.resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws SQLException {
        //源数据库
        Map map = new HashMap();
        map.put("driver", "com.mysql.jdbc.Driver");
        map.put("url", "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        map.put("username", "root");
        map.put("password", "123");

//        //模拟指定字段
//        String choose = "goods_name,id,goods_price";
//        String[] fileds = choose.split(",");
//
//        //1、扫描表结构
//        StringBuffer stringBuffer = JdbcUtils.scanningSourceDatabase(map, "test", "goods", null);
//
//        //2、建表
//        JdbcUtils.createTable(map, "goods", stringBuffer);
//
        //3、查询到的记录数
//        int dataCount = JdbcUtils.getCountFromSourceData(map, "goods");
//
//        //4、查询源数据库数据，并拼接insert语句
//        List<String> insertSqls = JdbcUtils.getSourceData(map, "goods", "new_goods", "");


//        //启动threadCount个线程
//        int threadCount = 50;
//        for (int i = 1; i <= threadCount; i++) {
//            //分配任务
//            if (null != exList) {
//                Thread thread = new Thread(new ThreadUtil(map, exList));
//                thread.start();
//            } else {
//                return;
//            }
//
//
//        }


    }

    /**
     * 创建threadCount个线程 均分记录数执行添加
     *
     * @return
     */
    public static List<String> AllocationThread(Map map, long dataCount, List<String> insertSqls, int threadCount) {
        Set<String> objects = new HashSet<>();              //Set去重
        ReentrantLock reentrantLock = new ReentrantLock();
        long exCount = dataCount / threadCount;              //等分执行数据
        reentrantLock.lock();                               //上锁 保证数组remove移除ConcurrentModificationException
        for (int j = 0; j < exCount; j++) {                 //exCount分配数组大小
            if (insertSqls.size() > 0) {
                objects.add(insertSqls.get(0));
                insertSqls.remove(0);
            }
        }
        if (insertSqls.size() > 0 && insertSqls.size() < exCount) {//不被threadCount整除部分
            insertSqls.forEach(i -> {
                objects.add(i);
            });
        }
        List<String> exList = new ArrayList<>(objects);         //  新建执行数组
        reentrantLock.unlock();                                 //放锁

        return exList;
    }


    public Map getDataSourceMap() {
        return dataSourceMap;
    }

    public void setDataSourceMap(Map dataSourceMap) {
        this.dataSourceMap = dataSourceMap;
    }

    public List<String> getResultList() {
        return resultList;
    }

    public void setResultList(List<String> resultList) {
        this.resultList = resultList;
    }

    public ThreadUtil(Map dataSourceMap, List<String> resultList) {
        this.dataSourceMap = dataSourceMap;
        this.resultList = resultList;
    }
}
